class Solution
{
public:
    int lengthOfLongestSubstring(string s)
    {
        int n = s.size();
        bool existChar[128] = {false};
        int begin = 0;
        int end = 0;
        int maxLen = 0;
        while (end < n)
        {
            if (existChar[s[end]])
            {
                while (s[begin] != s[end])
                {
                    existChar[s[begin]] = false;
                    ++begin;
                }
                ++begin;
                ++end;
            }
            else
            {
                existChar[s[end]] = true;
                ++end;
                maxLen = max(maxLen, end - begin);
            }
        }
        return maxLen;
    }
};